<?php


namespace App\Logic;


use App\Models\Question;
use App\server\EsServer;

class AnswerLogic
{
    /**
     * 问答信息展示列表
     */
    public static function Secondary(){
        return Question::list();
    }
    /**
     * 问答信息添加
     */
    public static function SecondaryAdd($request){
        $validated = $request->validate([
            'question' => 'required',
            'answer' => 'required',
            'keywords' => 'required',
            'tags' => 'required',
        ],[
            'question.required'=>'不能为空',
            'answer.required'=>'不能为空',
            'keywords.required'=>'不能为空',
            'tags.required'=>'不能为空',
        ]);
        $SecondaryModel=Question::create($validated);
        return $SecondaryModel;
    }
    /**
     * 问答信息删除
     */
    public static function SecondaryDel($fangId){
        if(empty($fangId))throw new \Exception('未接收到想要删除的房源问答信息');
        $executeDel=Question::del('id',$fangId);
        return $executeDel;
    }
    /**
     * 问答信息查找
     */
    public static function SecondarySeek($query){
        if(empty($query)){
            return Question::list();
        }
        $executeSeek=Question::Seek('question','keywords',$query);
        return $executeSeek;
    }
    /**
     * 问答详情
     */
    public static function SecondaryDetails($fangID){
        if(empty($fangID))throw new \Exception('未接收到想要查看的房源问答信息');
        $executeDetail=Question::detail('id',$fangID);
        return $executeDetail;
    }
    /**
     * 问答修改
     */
    public static function SecondaryUpdated($SecondaryMessageUpdated){
//        dd($SecondaryMessageUpdated);
        unset($SecondaryMessageUpdated['updated_at']);
        unset($SecondaryMessageUpdated['created_at']);
        unset($SecondaryMessageUpdated['deleted_at']);
        $fangID=$SecondaryMessageUpdated['id'];
        $SecondaryModel=Question::change('id',$fangID,$SecondaryMessageUpdated);
        return $SecondaryModel;
    }
    /**
     * 问答es搜索
     */
    public static function SecondarySearch($query){
        if(empty($query)){
            return Question::list();
        }
        $executeEsSearch=new EsServer();
        $EsMessage['data']=$executeEsSearch->searchEs('question',$query,'keywords');
        return $EsMessage;
    }
}
